
package com.xijia.example.task;

import com.xijia.example.manage.all.service.SiteService;
import io.github.wslxm.springbootplus2.core.utils.date.XjLocalDateTimeUtil;
import io.github.wslxm.springbootplus2.core.utils.error.XjErrorMsgUtil;
import io.github.wslxm.springbootplus2.starter.robot.service.RobotService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;

/**
 * 站点监控任务 (每小时执行一次)
 *
 * @author wangsong
 * @date 2023/06/15
 */
@Configuration
@EnableScheduling
@Slf4j
public class SiteMonitorTask {

    @Autowired
    private SiteService siteService;

    @Autowired
    private RobotService robotService;


    @Scheduled(cron = "0 0 0/1 * * ?")
//    @Scheduled(cron = "0/30 * * * * ?")
    public void run() {
        try {
            // 当前系统的所有站点检查
            siteService.siteChecksAll();
            // 指定 webhook2 源
            String timeStr = XjLocalDateTimeUtil.parse(LocalDateTime.now());
            robotService.sendMsg("webhook2", "【站点工具中心】站点检查执行完成 " +timeStr, null);
        } catch (Exception e) {
            String content = "站点检查执行出现异常" + XjErrorMsgUtil.getErrorStr(e);
            robotService.sendMsg("【站点工具中心】" + content);
        }
    }
}
